package com.zhy.sunmi.activity;

import android.content.Context;
import android.graphics.Color;
import android.hardware.display.DisplayManager;
import android.media.MediaRouter;
import android.os.Build;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;

import com.partner.tabtools.verticalTab.TabAdapter;
import com.partner.tabtools.verticalTab.TabView;
import com.partner.tabtools.verticalTab.VerticalTabLayout;
import com.zhy.sunmi.R;
import com.zhy.sunmi.fragment.product.ProductFragment;
import com.zhy.sunmi.fragment.report.ReportFragment;
import com.zhy.sunmi.fragment.sell.SellFragment;
import com.zhy.sunmi.fragment.setting.SettingFragment;
import com.zhy.sunmi.fragment.staff.StaffFragment;
import com.zhy.sunmi.myView.MyPresentation;
import com.zhy.sunmi.myView.MyViewPager;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import POSSDK.POSSDK;
import woyou.aidlservice.jiuiv5.IWoyouService;


/**
 * @author zhouhongyang
 */
public class HomeActivity extends AppCompatActivity {
    private List<String> tabsTitle = new ArrayList<>();
    private List<Fragment> fragments = new ArrayList<>();
    private MyViewPager vpAllTags;
    private FragmentPagerAdapter fragmentPagerAdapter;
    IWoyouService iWoyouService;
    private VerticalTabLayout verticalTabLayout;
    POSSDK possdk;



    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initFullScreen();
        setContentView(R.layout.activity_home);
//        registerReceiver();
        initView();
        initData();
        initAdapter();

//        showDisplayByMediaRouter();
//        showDisplayByDisplayManager();
    }

    private void showDisplayByDisplayManager() {
        DisplayManager displayManager = (DisplayManager) this.getSystemService(Context.DISPLAY_SERVICE);
        Display[] displays = displayManager.getDisplays(DisplayManager.DISPLAY_CATEGORY_PRESENTATION);
        if (displays.length > 0) {
            Display display = displays[0];
            if (display != null) {
                MyPresentation myPresentation = new MyPresentation(this, display);
                myPresentation.show();
            }
        } else {
            Toast.makeText(this, "Not Found Second Screen!", Toast.LENGTH_SHORT).show();
        }


    }



    private void initAdapter() {
        fragmentPagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }

            @Override
            public int getCount() {
                return fragments.size();
            }
        };

        vpAllTags.setAdapter(fragmentPagerAdapter);
        verticalTabLayout.setupWithViewPager(vpAllTags);

        TabAdapter adapter = new TabAdapter() {
            @Override
            public int getCount() {
                return tabsTitle.size();
            }

            @Override
            public TabView.TabBadge getBadge(int position) {
                return null;
            }

            @Override
            public TabView.TabIcon getIcon(int position) {
                TabView.TabIcon.Builder builder = new TabView.TabIcon.Builder().setIconGravity(Gravity.TOP)
                        .setIconSize(20, 20)
                        .setIconMargin(0);
                switch (position) {
                    case 0:
                        builder.setIcon(R.mipmap.sell, R.mipmap.sell);
                        break;
//                    case 1:
//                        builder.setIcon(R.mipmap.reporting, R.mipmap.reporting);
//                        break;
//                    case 2:
//                        builder.setIcon(R.mipmap.product, R.mipmap.product);
//                        break;
//                    case 1:
//                        builder.setIcon(R.mipmap.staff, R.mipmap.staff);
//                        break;
                    case 1:
                        builder.setIcon(R.mipmap.setting, R.mipmap.setting);
                        break;
                }

                return builder.build();

            }

            @Override
            public TabView.TabTitle getTitle(int position) {

                return new TabView.TabTitle.Builder()
                        .setContent(tabsTitle.get(position))
                        .setTextSize(14)
                        .setTextColor(0xff6692b0, 0xFF3A4955)
                        .build();
            }

            @Override
            public int getBackground(int position) {
                return getResources().getColor(R.color.white2);
            }
        };

        verticalTabLayout.setTabAdapter2(adapter);
    }

    private void initData() {
//        tabsTitle = Arrays.asList("Sell", "Report", "Product", "Staff","Setting");
//        tabsTitle = Arrays.asList("Sell", "Staff","Setting");
        tabsTitle = Arrays.asList("Sell", "Setting");

        fragments.add(new SellFragment());
//        fragments.add(new ReportFragment());
//        fragments.add(new ProductFragment());
//        fragments.add(new StaffFragment());
        fragments.add(new SettingFragment());

//        for (String title : tabsTitle){
//            Fragment fragment=new SellFragment();
//            fragments.add(fragment);
//        }

    }

    private void initView() {
        verticalTabLayout = findViewById(R.id.verticalTabLayout);
        vpAllTags = findViewById(R.id.viewPager);
        findViewById(R.id.imageView2).setOnClickListener(v -> {

        });
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private void initFullScreen() {
        if (getSupportActionBar() != null) {
            getSupportActionBar().hide();
        }
        //隐藏虚拟按键，并且全屏
        if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) {
            // lower api
            View v = this.getWindow().getDecorView();
            v.setSystemUiVisibility(View.GONE);
        } else if (Build.VERSION.SDK_INT >= 19) {

            Window _window = getWindow();
            WindowManager.LayoutParams params = _window.getAttributes();
            params.systemUiVisibility = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE;
            _window.setAttributes(params);
        }
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        // 部分机型的statusbar会有半透明的黑色背景
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        // SDK21
        getWindow().setStatusBarColor(Color.TRANSPARENT);
        //设置状态栏文字颜色
//        setAndroidNativeLightStatusBar(this,true);
        View decor = this.getWindow().getDecorView();

        decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);

    }







}